<?php

session_start();
if(!array_key_exists("user", $_SESSION) || $_SESSION['account'] != 2){
	header("Location: ../common/redirect.php");
	exit;
}

if(!array_key_exists("id", $_GET)){
	header("Location: homework.php");
	exit;
} else {
	$id = preg_replace("/[^a-zA-Z0-9\s]/", "", $_GET['id']);
}
include_once("../class/db.php");
$db = new db();
$sql = "SELECT uid, title, deliver_method, homework_end_date FROM course_content WHERE id = $id AND owner = {$_SESSION['user']} AND contenttype = 'homework'";
$homeworks = $db->execute($sql, "all");

$sql = "SELECT grade_format FROM course WHERE link IN ( SELECT link FROM course_modules WHERE id IN (SELECT uid FROM course_content WHERE id = $id ))";
$grade_format = $db->execute($sql, "onerow");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PIE EDUCATION | Educational Network</title>
<link rel="stylesheet" type="text/css" href="/css/style-student.css" />
<link href="/css/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="/js/index.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script language="Javascript1.2">
<!--
setTimeout('location.href="/logout.php?expire=1"',600000);
// -->
</script>
<script type="text/javascript">
$(document).ready(function(){ 
	
	$("#accordion").accordion({ autoHeight: false });

});	
</script>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-35230372-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#000000"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="350" height="30">&nbsp;</td>
        <td align="right" valign="bottom"><a href="class.php" class="top-menu" style="color:#999">MY CLASS</a> <a href="schedule.php" class="top-menu" style="color:#999">CALENDAR</a> <a href="homework.php" class="top-menu" style="color:#999">HOMEWORK</a> <a href="test.php" class="top-menu" style="color:#999">EXAMS</a> <a href="grades.php" class="top-menu" style="color:#999">GRADES</a> <a href="tools.php" class="top-menu" style="color:#999">TOOLS</a> <a href="/logout.php" class="top-menu" style="color:#999">SIGN OUT</a></td>
      </tr>
	  <tr>
        <td>&nbsp;</td>
        <td align="right" valign="bottom"><a href="/profile/" class="login-menu" style="color:#FFF"><?php echo $_SESSION['name'].' '.$_SESSION['lname']; ?></a> <a href="/common/redirect.php" class="login-menu" style="color:#FFF">My Documents</a> <a href="/network/" class="login-menu" style="color:#FFF">My Wall</a> <a href="/common/settings.php" class="login-menu" style="color:#FFF">Settings</a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="30" style="background-image:url(/images/top-bg-student.png); background-repeat:repeat-x">&nbsp;</td>
  </tr>
  <tr>
    <td height="550" align="center" valign="top">
    
    <table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <?php
	  	if($homeworks != false){
	  ?>
      <tr>
        <td><div id="accordion">
		 <?php
            foreach($homeworks as $data){
         ?>
        <h3><a href="#"><?php echo $data['title']; ?></a></h3>
        <div>
            
            <table width="100%" border="0" cellspacing="4" cellpadding="4">
              <tr>
                <td width="38%" align="center" bgcolor="#CCCCCC" style="font-family:Arial, Helvetica, sans-serif"><strong>Student Name</strong></td>
                <td width="20%" align="center" bgcolor="#CCCCCC" style="font-family:Arial, Helvetica, sans-serif"><strong>Deliver Method</strong></td>
                <td width="14%" align="center" bgcolor="#CCCCCC" style="font-family:Arial, Helvetica, sans-serif"><strong>Document</strong></td>
                <td width="14%" align="center" bgcolor="#CCCCCC" style="font-family:Arial, Helvetica, sans-serif"><strong>Sent Date</strong></td>
                <td width="14%" align="center" bgcolor="#CCCCCC" style="font-family:Arial, Helvetica, sans-serif"><strong>Grade</strong></td>
              </tr>
              <?php
			  $sql = "SELECT * FROM homeworks WHERE uid = $id";
			  $homework = $db->execute($sql, "all");
			  if($homework != false){
				  foreach($homework as $row){
				  echo '<tr>
					<td align="left" style="font-family:Arial, Helvetica, sans-serif">'.$row['username'].'</td>
					<td align="center" style="font-family:Arial, Helvetica, sans-serif">'.$data['deliver_method'].'</td>
					<td align="center" style="font-family:Arial, Helvetica, sans-serif"><a href="../documents/course/'.$row['document'].'" target="_blank"><img src="../images/icons/down-blue.gif" width="14" height="14" alt="Right click and Save as" title="Right click and Save as" border="0" /></a></td>
					<td align="center" style="font-family:Arial, Helvetica, sans-serif">'.$row['deliver_date'].'</td>
					<td align="center" style="font-family:Arial, Helvetica, sans-serif">
					<form id="form1" name="form1" method="post" action="">';
					
						switch($grade_format['grade_format']){
							case 1:
								$gradeaf = array(1 => "A+", 2 => "A", 3 => "A-", 4 => "B+", 5 => "B", 6 => "B-", 7 => "C+", 8 => "C", 9 => "C-", 10 => "D+", 11 => "D", 12 => "D-", 13 => "F",);
								echo '<select name="grade'.$row['id'].'" id="grade'.$row['id'].'" onchange="testing(this.id);">';
								echo '<option value="0" selected="selected">Grade Later</option>';
								foreach($gradeaf as $key => $value){
								  echo '<option value="'.$key.'">'.$value.'</option>';
								}
								echo '</select>';
								break;
							case 2:
								echo '<select name="grade'.$row['id'].'" id="grade'.$row['id'].'" onchange="testing(this.id);">';
								echo '<option value="0" selected="selected">Grade Later</option>';
								for($i = 1; $i < 101; $i++){
									  echo '<option value="'.$i.'">'.$i.'</option>';
								}
								echo '</select>';
								break;
						}
					echo '</form>
					</td>
				  </tr>';
				  }
			  } else {
				echo '<tr>
                <td colspan="5" align="center" style="font-family:Arial, Helvetica, sans-serif;font-size:12px;color:#666666;" height="30">No homeworks has been delivered yet or the homeworkds has been sent by email.</td>
                </tr>';
			  }
              ?>
            </table>
            
        </div>
        <?php
            }
        ?>
</div></td>
      </tr>
      <?php
		} else {
	  ?>
      <tr>
		<td align="center" height="80"><div style="font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#666666;"><strong>No Homeworks Found</strong></div><div style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#999999;">Create a new homework using the button below</div></td>
	  </tr>
      <tr>
        <td align="center"><input type="button" class="submit-center" onclick="location.href='homework-create.php'" value='Create a New Homework' tabindex="10" /></td>
      </tr>
      <?php
		}
	  ?>
</table></td>
      </tr>
  <tr>
    <td height="30" align="center" style=" background-image:url(../images/bottom-bg-student.png); background-repeat:repeat-x;">&nbsp;</td>
  </tr>
  <tr>
    <td height="50" align="center" bgcolor="#000000" style="color:#fff; font-family:Arial, Helvetica, sans-serif">PIE.EDU All Rights Reserved 2012<br />
      <a href="/common/privacy.php" class="bottom-menu" style="color:#999">Privacy Policy</a> | <a href="/common/terms.php" class="bottom-menu"  style="color:#999">Terms of Use</a> | <a href="/help/main.php" class="bottom-menu" style="color:#999">Help Center</a> | <a href="/common/jobs.php" class="bottom-menu" style="color:#999">Jobs</a></td>
  </tr>
    </table>
</body>
</html>